package com.embhealth.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.embhealth.constant.MessageConstant;
import com.embhealth.constant.RedisConstant;
import com.embhealth.entity.PageResult;
import com.embhealth.entity.QueryPageBean;
import com.embhealth.entity.Result;
import com.embhealth.pojo.CheckGroup;
import com.embhealth.pojo.Setmeal;
import com.embhealth.service.SetmealService;
import com.embhealth.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Reference
    private SetmealService setmealService;
    @Autowired
    private JedisPool jedisPool;
    /**
     * 图像文件上传
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        //获取源文件文件名
        String originalFilename = imgFile.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");//获取最后一个.的位置
        String extention = originalFilename.substring(index);//动态截取最后一段文件名后缀
        String fileName = UUID.randomUUID().toString()+extention;
        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
    }

    /**
     * 新增套餐
     * @param checkgroupIds
     * @param setmeal
     * @return
     */
    @RequestMapping("/add")
    public Result add(Integer[] checkgroupIds, @RequestBody Setmeal setmeal){
        try{
            setmealService.add(setmeal,checkgroupIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return setmealService.findPage(queryPageBean);
    }

    /**
     * 编辑表单回显
     * @param id
     * @return
     */
    @RequestMapping("findById")
    public Result findById(Integer id){
        try {
            Setmeal setmeal=setmealService.findById(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,setmeal);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 根据套餐id回显关联的检查组
     * @param id
     * @return
     */
    @RequestMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(Integer id){
        try {
            List<Integer> list=setmealService.findCheckGroupIdsBySetmealId(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * 更新套餐信息
     * @param checkgroupIds
     * @param setmeal
     * @param imgurlchange
     * @return
     */
    @RequestMapping("edit")
    public Result edit(Integer[] checkgroupIds, @RequestBody Setmeal setmeal,String imgurlchange){
        try{
            setmealService.edit(setmeal,checkgroupIds);
            //先判断旧文件名是否在set集合中，在就删除
            if (imgurlchange!=null && jedisPool.getResource().sismember(RedisConstant.SETMEAL_PIC_DB_RESOURCES, imgurlchange)){
                jedisPool.getResource().hdel(RedisConstant.SETMEAL_PIC_DB_RESOURCES,imgurlchange);
            }
            QiniuUtils.deleteFileFromQiniu(imgurlchange);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 删除套餐信息
     * @param id
     * @param img
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id,String img){
        try {
            setmealService.delete(id);
            QiniuUtils.deleteFileFromQiniu(img);
            //从缓存中删除照片信息，删除前进行判断
            if (img!=null && jedisPool.getResource().sismember(RedisConstant.SETMEAL_PIC_DB_RESOURCES, img)){
                jedisPool.getResource().hdel(RedisConstant.SETMEAL_PIC_DB_RESOURCES,img);
            }
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

}
